﻿using MC;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Station.Inspect
{
    class StaConfig
    {
        public double codex2;
        public double codey2;
        public double coder2;
        //
        public double codex1;
        public double codey1;
        public double coder1;
        //搬运2原点
        public TargetPos PosA1;
        public TargetPos PosB1;
        public TargetPos PosA2;
        public TargetPos PosB2;
        //光源控制器
        public string lightCOM { get; set; } = "COM10";
        public int lightChannel { get; set; } = 0;
        public int brightnessTop1 { get; set; } = 0;
        public int brightnessBottom1 { get; set; } = 0;
        public int brightnessTop2 { get; set; } = 0;
        public int brightnessBottom2 { get; set; } = 0;
        //---
        public double Pixel2mm { get; set; } = 75;
        public double Pixel2mmB { get; set; } = 75;

        public double Gain { get; set; } = 0.0000;
        public int ExposureTime { get; set; } = 5000;
        public int CellNum { get; set; } = 100;
        public double OrgTopZ1 { get; set; } = 0;
        public double OrgBottomZ1 { get; set; } = 0;
        public double OrgTopZ2 { get; set; } = 0;
        public double OrgBottomZ2 { get; set; } = 0;
        public bool EnableAdjust { get; set; } = false;

        private static StaConfig instance;
        private static object instanceLock = new object();
        public static StaConfig Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (instanceLock)
                    {
                        if (instance == null)
                        {
                            instance = new StaConfig();
                        }
                    }
                }
                return instance;
            }
        }

        public void Load(string savaPath = "")
        {
            try
            {
                string path;
                if (string.IsNullOrEmpty(savaPath))
                {
                    path = Application.StartupPath + "\\staConfig.json";
                }
                else
                {
                    path = savaPath + "\\staConfig.json";
                }
                string str = File.ReadAllText(path);
                var config = JsonConvert.DeserializeObject<StaConfig>(str);
                if (config != null)
                {
                    instance = config;
                }
                Update();
            }
            catch (Exception ex)
            {
                Save(StaInspect.Instance.loadPath);
                MessageBox.Show(ex.Message);
            }
        }
        public void Save(string savaPath = "")
        {
            try
            {
                string path;
                Update();
                if (string.IsNullOrEmpty(savaPath))
                {
                    path = Application.StartupPath + "\\staConfig.json";
                }
                else
                {

                    path = savaPath + "\\staConfig.json";
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }

                }

                string str = JsonConvert.SerializeObject(Instance);
                File.WriteAllText(path, str);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void Update()
        {
            var sta = StaInspect.Instance;
            sta.PrjList[0].Pixel2mm.x = Pixel2mm;
            sta.PrjList[0].Pixel2mm.y = Pixel2mm;
            sta.PrjList[0].Pixel2mm.z = Pixel2mm;
            sta.PrjList[1].Pixel2mm.x = Pixel2mm;
            sta.PrjList[1].Pixel2mm.y = Pixel2mm;
            sta.PrjList[1].Pixel2mm.z = Pixel2mm;

            sta.Rbt.Pixel2mm.x = Pixel2mm;
            sta.Rbt.Pixel2mm.y = Pixel2mm;
            sta.Rbt.Pixel2mm.z = Pixel2mm;

            sta.config.CellNum = CellNum;
            sta.config.EnableAdjust = EnableAdjust;
            //
            sta.PrjList[0].sketchOrig.z = OrgTopZ1;
            sta.PrjList[1].sketchOrig.z = OrgBottomZ1;


            //
            sta.PrjList[1].Pixel2mm.x = Pixel2mmB;
            sta.PrjList[1].Pixel2mm.y = Pixel2mmB;

            //
            sta.PrjList[0].brightness = brightnessTop1;
            sta.PrjList[1].brightness = brightnessBottom1;
            //PosA1 = sta.PrjList[0].sketchOrig;
            //PosB1 = sta.PrjList[1].sketchOrig;
            PosA1.z = OrgTopZ1;
            PosB1.z = OrgBottomZ1;
            PosA2.z = OrgTopZ2;
            PosB2.z = OrgBottomZ2;

            //sta.Rbt.Orig.z = sta.Prj.sketchOrig.z;
            if (ExposureTime > 0)
            {
                sta.Camera.SetExposureTime(ExposureTime);
            }
            sta.Camera.SetGain(Gain);
        }
    }
}
